
import 'package:flutter/material.dart';
import 'package:flutter_deer/game/iview/game_list_iview.dart';
import 'package:flutter_deer/game/models/game_item_entity.dart';
import 'package:flutter_deer/game/presenter/game_list_presenter.dart';
import 'package:flutter_deer/game/provider/game_list_page_provider.dart';
import 'package:flutter_deer/mvp/base_page.dart';
import 'package:flutter_deer/mvp/power_presenter.dart';
import 'package:flutter_deer/res/constant.dart';
import 'package:flutter_deer/widgets/fadein_text.dart';
import 'package:flutter_deer/widgets/my_refresh_list.dart';
import 'package:flutter_deer/widgets/state_layout.dart';
import 'package:flutter_deer/util/other_utils.dart';
import 'package:flutter_deer/widgets/my_card.dart';
import 'package:flutter_deer/util/image_utils.dart';
import 'package:flutter_deer/widgets/load_image.dart';
import 'package:flutter_deer/res/resources.dart';
import 'package:flutter_gen/gen_l10n/deer_localizations.dart';
import 'package:lottie/lottie.dart';
import 'package:provider/provider.dart';
import 'package:text_scroll/text_scroll.dart';

import '../game_router.dart';

class GameListPage extends StatefulWidget {

  const GameListPage({
    super.key,
    required this.index
  });

  final int index;

  @override
  _GameListPageState createState() => _GameListPageState();
}

class _GameListPageState extends State<GameListPage> with BasePageMixin<GameListPage, PowerPresenter>,TickerProviderStateMixin implements GameListIMvpView {

  late GameListPagePresenter gameListPagePresenter;
  GameListPageProvider provider = GameListPageProvider();
  @override
  PowerPresenter<dynamic> createPresenter() {
    final PowerPresenter<dynamic> powerPresenter = PowerPresenter<dynamic>(this);
    gameListPagePresenter = new GameListPagePresenter();
    powerPresenter.requestPresenter([gameListPagePresenter]);
    return powerPresenter;
  }

  @override
  void setGameItemPage(GameItemPageEntity? pageEntity){
    provider.setGameItemPage(pageEntity);
  }

  @override
  void initState() {
    super.initState();
    if(mounted) {
      _onRefresh();
    }
  }

  @override
  void dispose() {
    // provider.dispose();
    super.dispose();
  }

  @override
  void didChangeDependencies(){
    super.didChangeDependencies();
  }

  Future<dynamic> _onRefresh() async {
    Future.delayed(Duration(seconds: 1),()=>{
      gameListPagePresenter.getGameList(widget.index)
    });
  }

  Future<dynamic> _loadMore() async {
    gameListPagePresenter.getMoreGameList(widget.index, provider.currentPage!+1);
  }

  @override
  Widget build(BuildContext context) {
    final TextStyle? textTextStyle = Theme.of(context).textTheme.bodyMedium?.copyWith(fontSize: Dimens.font_sp12);
    return ChangeNotifierProvider<GameListPageProvider>(
        create: (_) => provider,
        child: Consumer<GameListPageProvider>(
        builder: (_,provider,child)
    {
      if(provider.stateType==StateType.loading){
        return StateLayout(type: StateType.loading);
      }
      if(provider.list!.length>0) {
        return DeerListView(
            itemCount: provider.list!.length!,
            stateType: provider.stateType!,
            onRefresh: _onRefresh,
            loadMore: _loadMore,
            hasMore: provider.hasMore!,
            itemBuilder: (_, index) {
              final GameItemEntity item = provider.list!.elementAt(index);
              return Padding(
                  padding: const EdgeInsets.symmetric(
                      horizontal: 10.0, vertical: 5),
                  child: MyCard(
                      child: Padding(
                          padding: const EdgeInsets.symmetric(horizontal: 5),
                          child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Gaps.vGap5,
                              Row(
                                mainAxisAlignment: MainAxisAlignment
                                    .spaceBetween,
                                children: [
                                  Container(
                                    width: MediaQuery.of(context).size.width/2-50,
                                    child: TextScroll(item.name.nullSafe,mode: TextScrollMode.endless,pauseBetween: Duration(milliseconds: 500),)
                                  ),
                                  Text('${DeerLocalizations.of(context)!.playingTime.nullSafe}:' + item!.playDate.nullSafe.substring(5) + ' ' +
                                      item.playTime.nullSafe)
                                ],
                              ),
                              Gaps.vGap5,
                              Divider(height: 1.0, indent: 0.0),
                              Gaps.vGap10,
                              Row(
                                mainAxisAlignment: MainAxisAlignment
                                    .spaceEvenly,
                                crossAxisAlignment: CrossAxisAlignment.center,
                                children: [
                                  Container(
                                    width: (MediaQuery.of(context).size.width-80)/2,
                                    child: Column(
                                      children: [
                                        Image(image: ImageUtils.getImageProvider(
                                            Constant.imageServerHost +
                                                '${item.teamOne!.image.nullSafe}',
                                            holderImg: 'shop/tx'),
                                          width: 30,
                                          height: 30,),
                                        Gaps.vGap12,
                                        TextScroll(item.teamOne!.name.nullSafe,mode: TextScrollMode.endless,pauseBetween: Duration(milliseconds: 500),)
                                      ],
                                    ),
                                  ),
                                  LoadAssetImage(
                                      'logo', format: ImageFormat.gif,
                                      width: 50,
                                      height: 50),
                                  Container(
                                    width: (MediaQuery.of(context).size.width-80)/2,
                                    child: Column(
                                      children: [
                                        Image(image: ImageUtils.getImageProvider(
                                            Constant.imageServerHost +
                                                '${item.teamTwo!.image.nullSafe}',
                                            holderImg: 'shop/tx'),
                                          width: 30,
                                          height: 30,),
                                        Gaps.vGap12,
                                        TextScroll(item.teamTwo!.name.nullSafe,mode: TextScrollMode.endless,pauseBetween: Duration(milliseconds: 500),)
                                      ],
                                    ),
                                  ),
                                ],
                              ),
                              Gaps.vGap5,
                              Visibility(visible: item.competitionResult != '',
                                child: Divider(height: 1.0, indent: 0.0),),
                              Gaps.vGap5,
                              Visibility(visible: item.competitionResult != '',
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.end,
                                  children: [
                                    Text('${DeerLocalizations.of(context)!.gameResult.nullSafe}:${item.competitionResult}')
                                  ],
                                ),),
                              Gaps.vGap5,
                            ],
                          )
                      )
                  )
              );
            }
        );
      }else{
        return Container(
          width: MediaQuery.of(context).size.width,
          child: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Lottie.asset('assets/lottie/no_football_game.json',width: MediaQuery.of(context).size.width/3,fit: BoxFit.cover),
                Gaps.vGap8,
                FadeInText(text: '${DeerLocalizations.of(context)!.noGame.nullSafe}')
              ],
            ),
          ),
        );
      }
    }));
  }
  // @override
  // bool get wantKeepAlive => false;
}